package com.springmvc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.springmvc.entity.Items;
import com.springmvc.entity.ItemsQueryVo;
import com.springmvc.service.ItemsService;
import com.springmvc.valid.ValidGroup1;
import com.springmvc.valid.ValidGroup2;

/**
 * @author Administrator
 *
 */
@Controller
//为了窄化url，对url进行分类管理，在此处相当于根路径， 最终的访问路径： 根路径 + 子路径
//此时查询商品列表的访问路径： /items/queryItems.action
@RequestMapping("/items")
public class ItemsController {
	@Autowired
	private ItemsService itemsService;
	
	//查询商品列表
	//@RequestMapping实现了对queryItems方法和url进行映射， 一个方法对应一个url
	@RequestMapping("/queryItems")
	public String queryItems(Model model,ItemsQueryVo itemsQueryVo) throws Exception {
		List<Items> items = itemsService.queryItemsListByCondidition(itemsQueryVo);
		
		//设置数据,相当于在servlet中使用 request.setAttribute("itemsList", items)
		model.addAttribute("itemsList", items);
		model.addAttribute("itemsQueryVo", itemsQueryVo);
		
		return "items/itemsList";
	}
	
	//修改商品，进入商品修改页面
	@RequestMapping(value = "/form", method = {RequestMethod.POST,RequestMethod.GET})
	//@RequestParam中可以设置默认值，如果id参数没有传入，则使用默认值进行绑定
	public String form(Model model, @RequestParam(value="id", required=true, defaultValue="1") Integer items_id) throws Exception {
		//在进入表单页前，根据id查询商品对象信息
		Items items = itemsService.queryItemsById(items_id);
		
		//设置数据
		model.addAttribute("items", items);
		
		return "items/editItems";
	}
	
	//保存
	@RequestMapping("/save")
	//在pojo中添加@Validated该注解，完成对该bean的验证，使用BindingResult接口获取错误异常现象
	// 注意： @Validated和BindingResult必须是配对出现，并且Validated在前，BindingResult在后的配置顺序
	public String save(Model model,
			@Validated(value = {ValidGroup1.class, ValidGroup2.class}) Items items, 
			BindingResult bindingResult) throws Exception {
		//在完成更新之前获取错误信息
		if (bindingResult.hasErrors()) {
			//输出错误信息
			List<ObjectError> errors =  bindingResult.getAllErrors();
			for (ObjectError objectError : errors) {
				System.out.println(objectError.getDefaultMessage());
			}
			//将错误信息在页面中显示
			model.addAttribute("errors", errors);
			
			return "items/editItems";
		}
		
		//调用service，完成对象的保存、更新操作
		itemsService.udpateItemsById(items);
		
		//重定向
		return "redirect:queryItems.action";
		
		//转发
		//return "forward:queryItems.action";
	}
	
	//进入批量修改的页面
	@RequestMapping("/editAll")
	public String editAll(Model model,ItemsQueryVo itemsQueryVo) throws Exception {
		List<Items> items = itemsService.queryItemsListByCondidition(itemsQueryVo);
		
		model.addAttribute("itemsList", items);
		
		return "items/editItemsList";
	}
	
	//批量提交
	@RequestMapping("/saveAll")
	public String saveAll(ItemsQueryVo itemsQueryVo) throws Exception {
		//调用service，完成批量修改
		
		return "success";
	}
	
}
